/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.distrimas.senta.dominio.compras.ordencompra;

import com.distrimas.senta.dominio.compras.proveedores.ProveedorDAO;
import com.distrimas.senta.dominio.compras.proveedores.ProveedorDTO;
import com.distrimas.senta.dominio.contabilidad.nomina.EmpleadoDAO;
import com.distrimas.senta.servicios.comun.Validador;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Date;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author lis
 */
public class GenerarOCController extends HttpServlet {
   
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    private OrdenCompraDTO ordenCompra;
    private Date fecha;
    private ProveedorDAO proveedor;
    private ProveedorDTO proveedor2;
    private OrdenCompraDAO ordenCompraDao;
    //private FacadeContabilidad facadeCont;
    private EmpleadoDAO responsable;
  /*  protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
       // response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {

            String id = request.getParameter("idOrden");
            String nit = request.getParameter("nit");
            String producto = request.getParameter("producto");
            String cantidad = request.getParameter("cantidad");
            String fecha = request.getParameter("fecha");
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet GenerarOCController</title>");
            out.println("</head>");
            out.println("<body>");
            out.println("<h1> Orden de compra </h1>");
            out.print("<br>id = " + id);
            out.print("<br>nit = " + nit);
            out.print("<br>producto = " + producto);
            out.print("<br>cantidad = " + cantidad);
            out.print("<br>fecha = " + fecha);
        } finally { 
            out.close();
        }
    }*/

    protected void ingresarOrdenCompra(HttpServletRequest request, HttpServletResponse response )
    throws ServletException, IOException, IllegalArgumentException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            boolean ingreso=true,validaciones=false;;
            ordenCompra = new OrdenCompraDTO();
            ordenCompraDao= OrdenCompraDAO.getOrdenCompraDAO();
            proveedor= ProveedorDAO.getProveedorDAO();
            

            ordenCompra.setCodigo(request.getParameter("idOrden"));
            ordenCompra.setEstado("Generada");
            ordenCompra.setCalificacion(6f);
            proveedor2=proveedor.consultarProveedor(request.getParameter("nit"));
            ordenCompra.setProveedor(proveedor2);
            ordenCompra.setResponsable(proveedor2.getAuxiliarencargado());
            //ordenCompra.getResponsableAnular(null);
            ordenCompra.setFecha(Date.valueOf(request.getParameter("fecha")));
            ingreso = ordenCompraDao.ingresarOrdenCompra(ordenCompra);
            if (ingreso==true){
                out.println("<html>");
                out.println("<head>");
                out.println("<title>Generar OC</title>");
                out.println("</head>");
                out.println("<body>");
                out.println("<h1> Ingreso la orden de compra exitosamente"+"</h1>");
                out.println("</body>");
                out.println("</html>");

            }else{
                out.println("<html>");
                out.println("<head>");
                out.println("<title>ERROR</title>");
                out.println("</head>");
                out.println("<body>");
                out.println("<h1>Error al ingresar la OC revise los datos ingresados"+"</h1>");
                out.println("</body>");
                out.println("</html>");
            }
        }finally {
            out.close();
        }
    }
    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
          //   ingresarOrdenCompra(request, response);
    }
    

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
         ingresarOrdenCompra(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
